/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, Collection, template, Model, dialog) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            var _this = this;
            //分类
            $.ajax({
                url: _global.url.api + 'get_receiver',
                type: 'get',
                async: false,
                success: function (res) {
                    if (res.code == 0) {
                        _this.set('receivers', res.data);
                    }
                }
            });
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var that = this;
            this.collection = new Collection({
                url: _global.url.api + 'my_message',
                model: {
                    url: _global.url.api + 'my_message',
                    event: {
                        '.item-view:click': 'view',
                        '.item-read:click': 'read',
                    },

                    view: function (e) {
                        e.preventDefault();
                        var _this = this;
                        var data = _this.data;
                        if (data.is_read == 0) {
                            _this.data.id = data.id;
                            _this.data.status = 1;
                            if (!_this.put()) {
                                return false;
                            }
                            that.collection.reload();
                        }

                        _this.dialog = dialog({
                            title: data.title,
                            width: 700,
                            content: template('view-tpl', data),
                            ok: function () {
                            },
                        }).show();
                    },
                    read: function (e) {
                        e.preventDefault();
                        var _this = this;
                        var data = _this.data;
                        if (data.is_read == 0) {
                            _this.data.id = data.id;
                            _this.data.status = 1;
                            if (!_this.put()) {
                                return false;
                            }
                            that.collection.reload();
                        }
                    }
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },
        set: function ($k, $v) {
            this[$k] = $v;
        },
        event: function () {
            var _this = this;
            this.data = {};
            //保存
            $('.btn-all-read').on('click', function (e) {
                e.preventDefault();
                var data = {
                    status: 1,
                };
                if (!new Model({
                        url: _global.url.api + 'my_message',
                        data: data,
                    }).put()) {
                    return false;
                }
                ;
                _this.collection.reload();
            });

            $('.btn-read').on('click', function (e) {
                e.preventDefault();
                var status = $(this).attr('rel');
                _this.collection.condition.is_read = status;
                _this.collection.reload();
            })

            $('.btn-add').on('click', function (e) {
                e.preventDefault();
                _this.data = {};
                dialog({
                    'title': '发送消息',
                    'width': 700,
                    'content': template('add-tpl', {data: _this.receivers}),
                    ok: function () {
                        _this.data.title = $('input[name=title]').val();
                        _this.data.users = $('textarea[name=users]').val();
                        _this.data.content = $('textarea[name=content]').val();
                        _this.data.receiver_type = $('input[name=receiver_type]:checked').val();
                        if (!new Model({
                                url: _global.url.api + 'my_message',
                                data: _this.data,
                                valid_config: {
                                    title: 'required',
                                    receiver_type: 'required',
                                    content: 'required',
                                }
                            }).post()) {
                            return false;
                        }
                        //重新加载
                        _this.collection.reload();
                    }, cancel: function () {
                    }
                }).show();

                $('.item-cate').on('change', function (e) {
                    e.preventDefault();
                    var status = $(this).val();
                    if (status == 3) {
                        $('.users').show();
                    } else {
                        $('.users').hide();
                    }
                })

                $('#add-receiver').on('keyup', function (e) {
                    e.preventDefault();
                    //<span class="label label-info mr10">dd</span>
                    if (e.keyCode == 13) {
                        var inputValue = $(this).val();
                        var rule = /^\d+$/;
                        var isOk = rule.test(inputValue);
                        if (!inputValue || inputValue == 'undefined' || isOk == false) {
                            return false;
                        }
                        var users = $('textarea[name=users]').val();
                        var nameLabel = $('#nameLabel').html();
                        $('#nameLabel').html(nameLabel + '<span class="label label-info mr10">' + _this.receivers[inputValue] + '</span>');

                        if (!users || users == 'undefined') {
                            $('textarea[name=users]').text(inputValue);
                        } else {
                            $('textarea[name=users]').text(users + ',' + inputValue);
                        }
                        $(this).val('');
                    }

                })
            })

        },


        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        },
    };

    new index();

});